// merchants/exportRecords/index.js
const app = getApp();
const utils = require('../../../utils/util.js');
Page({

  /**
   * 页面的初始数据
   */
  data: {
    selected: 1,
    nowDate: '',
    startDate: '',
    endDate: '',
    type: '', //coupon优惠券，carditem消费卡
    upfilelist: '',
    cardId: 0,
    email: '',
    parentUid: 0,//经营报表需要的参数
    childUid: 0,//经营报表需要的参数
    linetype: 1,//线上支付，2线下支付
    state: 0, // 1 供货订单, 2 带货订单
  },
  selectExport: function(e) {
    var date = new Date();
    var nowDate = this.data.nowDate; //当天日期
    let selected = e.currentTarget.dataset.index;
    let startDate = this.data.startDate;
    let endDate = this.data.endDate;
    if (selected == 1) { //1昨天，2近一周，3近一个月，4自定义
      endDate = utils.formatTime(utils.jianDate(nowDate, 1)).substr(0, 10).replace(/\//g, "-");
      startDate = endDate;
    } else if (selected == 2) {
      endDate = utils.formatTime(utils.jianDate(nowDate, 1)).substr(0, 10).replace(/\//g, "-");
      startDate = utils.formatTime(utils.jianDate(endDate, 6)).substr(0, 10).replace(/\//g, "-");
    } else if (selected == 3) {
      endDate = utils.formatTime(utils.jianDate(nowDate, 1)).substr(0, 10).replace(/\//g, "-");
      startDate = utils.formatTime(utils.jianDate(endDate, 30)).substr(0, 10).replace(/\//g, "-");
    }
    this.setData({
      selected: selected,
      startDate: startDate,
      endDate: endDate
    })
  },

  //发送
  uploadExcel: function() {
    let that = this;
    var nowDate = that.data.nowDate;
    var startDate = that.data.startDate;
    var endDate = that.data.endDate;
    let email = that.data.email;
    let createUid = app.globalData.userInfo.Uid;
    let saleuid = 0;
    let jump = 0;
    //供货订单
    if(that.data.state == 1){
      jump = 12;
    }
    //带货订单
    if(that.data.state == 2){
      saleuid = app.globalData.userInfo.Uid;
      createUid = 0;
      jump = 12;
    }
    if (email==''){
      that.checkWrite('请输入邮箱');
      return;
    } else if (!email.match(/^[.a-zA-Z0-9_-]+@[a-zA-Z0-9_-]+(\.[a-zA-Z0-9_-]+)+$/)){
      that.checkWrite('邮箱格式不正确，请重新输入');
      return;
    }
    if (that.data.selected == 1) {
      endDate = startDate;
    }
  
    if (utils.compareDate(startDate, endDate)) {
      that.checkWrite("结束时间必须大于起始时间！");
      return;
    };
    if (utils.compareDate(endDate, nowDate)) {
      that.checkWrite("结束时间必须小于等于当天时间！");
      return;
    };
  
    wx.showLoading({title: '正在导出'})
    wx.request({
      url: app.globalData.host + '/api/tool/OrderToEmail?createUid=' + createUid + '&email='+ email + '&startTime=' + startDate + '&endTime=' + endDate +'&jump=' + jump + '&saleuid=' + saleuid ,
      success: function(res) {
        wx.hideLoading();
        if (res.data.Code == 0) {
          wx.showToast({
            title: '导出成功',
          })
        }
      }
    })
  },
  bindEmail: function(e) {
    let email = e.detail.value;
    this.setData({
      email: email
    })
  },

  cancelUpload: function() {
    wx.navigateBack({
      delta: 1
    })
  },
  getCardDetail: function() {
    var that = this;
    var cardId = wx.getStorageSync("cardId");
    if (!cardId) {
      cardId = that.data.cardId;
    }
    wx.request({
      url: app.globalData.host + '/api/Staff/GetInfo?id=' + cardId,
      success: function(res) {
        if (res.data.Code == 0) {
          that.setData({
            email: res.data.Data.Email
          })
        } else {
          wx.showToast({
            title: '信息获取失败',
            duration: 500
          })
          wx.redirectTo({
            url: '/pages/weijieke/cardList/index',
          })
        }
      }
    })
  },

  /**
   * 生命周期函数--监听页面加载
   */
  onLoad: function(options) {
    if (options.type){
      this.setData({
        type: options.type
      })
    }
    if (options.childUid){
      this.setData({
        childUid: options.childUid,
        parentUid: options.parentUid
      })
    }
    if (options.linetype){
      this.setData({
        linetype: options.linetype
      })
    }
    if (options.state){
      this.setData({
        state: options.state
      })
    }
    this.getCardDetail();
  },

  /**
   * 生命周期函数--监听页面显示
   */
  onShow: function() {
    var date = new Date();
    var endDate = utils.getCurrentToday().substr(0, 10);
    var startDate = utils.formatTime(utils.jianDate(endDate, 1)).substr(0, 10).replace(/\//g, "-");
    this.setData({
      startDate: startDate,
      endDate: endDate,
      nowDate: endDate
    })
  },
  bindDateChange: function(e) {
    this.setData({
      startDate: e.detail.value
    })
  },
  bindDateChange2: function(e) {
    this.setData({
      endDate: e.detail.value
    })
  },
  //填写判断
  checkWrite: function(str) {
    wx.showModal({
      title: '提示',
      content: str,
      showCancel: false
    })
  }
})